home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / SMTP.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  11.7 KB  |  529 lines

  1.  
  2. package Net::SMTP;
  3.  
  4. require 5.001;
  5.  
  6. use strict;
  7. use vars qw($VERSION @ISA);
  8. use Socket 1.3;
  9. use Carp;
  10. use IO::Socket;
  11. use Net::Cmd;
  12. use Net::Config;
  13.  
  14. $VERSION = do { my @r=(q$Revision: 2.9.1 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  15.  
  16. @ISA = qw(Net::Cmd IO::Socket::INET);
  17.  
  18. sub new
  19. {
  20.  my $self = shift;
  21.  my $type = ref($self) || $self;
  22.  my $host = shift if @_ % 2;
  23.  my %arg  = @_; 
  24.  my $hosts = defined $host ? [ $host ] : $NetConfig{smtp_hosts};
  25.  my $obj;
  26.  
  27.  my $h;
  28.  foreach $h (@{$hosts})
  29.   {
  30.    $obj = $type->SUPER::new(PeerAddr => ($host = $h), 
  31.                 PeerPort => $arg{Port} || 'smtp(25)',
  32.                 Proto    => 'tcp',
  33.                 Timeout  => defined $arg{Timeout}
  34.                         ? $arg{Timeout}
  35.                         : 120
  36.                ) and last;
  37.   }
  38.  
  39.  return undef
  40.     unless defined $obj;
  41.  
  42.  $obj->autoflush(1);
  43.  
  44.  $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  45.  
  46.  unless ($obj->response() == CMD_OK)
  47.   {
  48.    $obj->SUPER::close();
  49.    return undef;
  50.   }
  51.  
  52.  ${*$obj}{'net_smtp_host'} = $host;
  53.  
  54.  (${*$obj}{'net_smtp_domain'}) = $obj->message =~ /\A\s*(\S+)/;
  55.  
  56.  $obj->hello($arg{Hello} || "");
  57.  
  58.  $obj;
  59. }
  60.  
  61.  
  62. sub domain
  63. {
  64.  my $me = shift;
  65.  
  66.  return ${*$me}{'net_smtp_domain'} || undef;
  67. }
  68.  
  69. sub hello
  70. {
  71.  my $me = shift;
  72.  my $domain = shift ||
  73.           eval {
  74.             require Net::Domain;
  75.             Net::Domain::hostfqdn();
  76.            } ||
  77.         "";
  78.  my $ok = $me->_EHLO($domain);
  79.  my $msg;
  80.  
  81.  if($ok)
  82.   {
  83.    $msg = $me->message;
  84.  
  85.    my $h = ${*$me}{'net_smtp_esmtp'} = {};
  86.    my $ext;
  87.    foreach $ext (qw(8BITMIME CHECKPOINT DSN SIZE))
  88.     {
  89.      $h->{$ext} = 1
  90.     if $msg =~ /\b${ext}\b/;
  91.     }
  92.   }
  93.  else
  94.   {
  95.    $msg = $me->message
  96.     if $me->_HELO($domain);
  97.   }
  98.  
  99.  $ok && $msg =~ /\A(\S+)/
  100.     ? $1
  101.     : undef;
  102. }
  103.  
  104. sub _addr
  105. {
  106.  my $addr = shift || "";
  107.  
  108.  return $1
  109.     if $addr =~ /(<[^>]+>)/so;
  110.  
  111.  $addr =~ s/\n/ /sog;
  112.  $addr =~ s/(\A\s+|\s+\Z)//sog;
  113.  
  114.  return "<" . $addr . ">";
  115. }
  116.  
  117.  
  118. sub mail
  119. {
  120.  my $me = shift;
  121.  my $addr = _addr(shift);
  122.  my $opts = "";
  123.  
  124.  if(@_)
  125.   {
  126.    my %opt = @_;
  127.    my($k,$v);
  128.  
  129.    if(exists ${*$me}{'net_smtp_esmtp'})
  130.     {
  131.      my $esmtp = ${*$me}{'net_smtp_esmtp'};
  132.  
  133.      if(defined($v = delete $opt{Size}))
  134.       {
  135.        if(exists $esmtp->{SIZE})
  136.         {
  137.          $opts .= sprintf " SIZE=%d", $v + 0
  138.         }
  139.        else
  140.         {
  141.      carp 'Net::SMTP::mail: SIZE option not supported by host';
  142.         }
  143.       }
  144.  
  145.      if(defined($v = delete $opt{Return}))
  146.       {
  147.        if(exists $esmtp->{DSN})
  148.         {
  149.      $opts .= " RET=" . uc $v
  150.         }
  151.        else
  152.         {
  153.      carp 'Net::SMTP::mail: DSN option not supported by host';
  154.         }
  155.       }
  156.  
  157.      if(defined($v = delete $opt{Bits}))
  158.       {
  159.        if(exists $esmtp->{'8BITMIME'})
  160.         {
  161.      $opts .= $v == 8 ? " BODY=8BITMIME" : " BODY=7BIT"
  162.         }
  163.        else
  164.         {
  165.      carp 'Net::SMTP::mail: 8BITMIME option not supported by host';
  166.         }
  167.       }
  168.  
  169.      if(defined($v = delete $opt{Transaction}))
  170.       {
  171.        if(exists $esmtp->{CHECKPOINT})
  172.         {
  173.      $opts .= " TRANSID=" . _addr($v);
  174.         }
  175.        else
  176.         {
  177.      carp 'Net::SMTP::mail: CHECKPOINT option not supported by host';
  178.         }
  179.       }
  180.  
  181.      if(defined($v = delete $opt{Envelope}))
  182.       {
  183.        if(exists $esmtp->{DSN})
  184.         {
  185.      $v =~ s/([^\041-\176]|=|\+)/sprintf "+%02x", ord($1)/sge;
  186.      $opts .= " ENVID=$v"
  187.         }
  188.        else
  189.         {
  190.      carp 'Net::SMTP::mail: DSN option not supported by host';
  191.         }
  192.       }
  193.  
  194.      carp 'Net::SMTP::recipient: unknown option(s) '
  195.         . join(" ", keys %opt)
  196.         . ' - ignored'
  197.     if scalar keys %opt;
  198.     }
  199.    else
  200.     {
  201.      carp 'Net::SMTP::mail: ESMTP not supported by host - options discarded :-(';
  202.     }
  203.   }
  204.  
  205.  $me->_MAIL("FROM:".$addr.$opts);
  206. }
  207.  
  208. sub send      { shift->_SEND("FROM:" . _addr($_[0])) }
  209. sub send_or_mail  { shift->_SOML("FROM:" . _addr($_[0])) }
  210. sub send_and_mail { shift->_SAML("FROM:" . _addr($_[0])) }
  211.  
  212. sub reset
  213. {
  214.  my $me = shift;
  215.  
  216.  $me->dataend()
  217.     if(exists ${*$me}{'net_smtp_lastch'});
  218.  
  219.  $me->_RSET();
  220. }
  221.  
  222.  
  223. sub recipient
  224. {
  225.  my $smtp = shift;
  226.  my $ok = 1;
  227.  my $opts = "";
  228.  
  229.  if(@_ && ref($_[-1]))
  230.   {
  231.    my %opt = %{pop(@_)};
  232.    my $v;
  233.  
  234.    if(exists ${*$smtp}{'net_smtp_esmtp'})
  235.     {
  236.      my $esmtp = ${*$smtp}{'net_smtp_esmtp'};
  237.  
  238.      if(defined($v = delete $opt{Notify}))
  239.       {
  240.        if(exists $esmtp->{DSN})
  241.         {
  242.      $opts .= " NOTIFY=" . join(",",map { uc $_ } @$v)
  243.         }
  244.        else
  245.         {
  246.      carp 'Net::SMTP::recipient: DSN option not supported by host';
  247.         }
  248.       }
  249.  
  250.      carp 'Net::SMTP::recipient: unknown option(s) '
  251.         . join(" ", keys %opt)
  252.         . ' - ignored'
  253.     if scalar keys %opt;
  254.     }
  255.    else
  256.     {
  257.      carp 'Net::SMTP::recipient: ESMTP not supported by host - options discarded :-(';
  258.     }
  259.   }
  260.  
  261.  while($ok && scalar(@_))
  262.   {
  263.    $ok = $smtp->_RCPT("TO:" . _addr(shift) . $opts);
  264.   }
  265.  
  266.  return $ok;
  267. }
  268.  
  269. sub to { shift->recipient(@_) }
  270.  
  271. sub data
  272. {
  273.  my $me = shift;
  274.  
  275.  my $ok = $me->_DATA() && $me->datasend(@_);
  276.  
  277.  $ok && @_ ? $me->dataend
  278.        : $ok;
  279. }
  280.  
  281. sub expand
  282. {
  283.  my $me = shift;
  284.  
  285.  $me->_EXPN(@_) ? ($me->message)
  286.         : ();
  287. }
  288.  
  289.  
  290. sub verify { shift->_VRFY(@_) }
  291.  
  292. sub help
  293. {
  294.  my $me = shift;
  295.  
  296.  $me->_HELP(@_) ? scalar $me->message
  297.             : undef;
  298. }
  299.  
  300. sub close
  301. {
  302.  my $me = shift;
  303.  
  304.  return 1
  305.    unless (ref($me) && defined fileno($me));
  306.  
  307.  $me->_QUIT && $me->SUPER::close;
  308. }
  309.  
  310. sub DESTROY { shift->close }
  311. sub quit    { shift->close }
  312.  
  313.  
  314. sub _EHLO { shift->command("EHLO", @_)->response()  == CMD_OK }   
  315. sub _HELO { shift->command("HELO", @_)->response()  == CMD_OK }   
  316. sub _MAIL { shift->command("MAIL", @_)->response()  == CMD_OK }   
  317. sub _RCPT { shift->command("RCPT", @_)->response()  == CMD_OK }   
  318. sub _SEND { shift->command("SEND", @_)->response()  == CMD_OK }   
  319. sub _SAML { shift->command("SAML", @_)->response()  == CMD_OK }   
  320. sub _SOML { shift->command("SOML", @_)->response()  == CMD_OK }   
  321. sub _VRFY { shift->command("VRFY", @_)->response()  == CMD_OK }   
  322. sub _EXPN { shift->command("EXPN", @_)->response()  == CMD_OK }   
  323. sub _HELP { shift->command("HELP", @_)->response()  == CMD_OK }   
  324. sub _RSET { shift->command("RSET")->response()        == CMD_OK }   
  325. sub _NOOP { shift->command("NOOP")->response()        == CMD_OK }   
  326. sub _QUIT { shift->command("QUIT")->response()        == CMD_OK }   
  327. sub _DATA { shift->command("DATA")->response()        == CMD_MORE } 
  328. sub _TURN { shift->unsupported(@_); }                      
  329.  
  330. 1;
  331.  
  332. __END__
  333.  
  334. =head1 NAME
  335.  
  336. Net::SMTP - Simple Mail Transfer Protocol Client
  337.  
  338. =head1 SYNOPSIS
  339.  
  340.     use Net::SMTP;
  341.     
  342.     $smtp = Net::SMTP->new('mailhost');
  343.     $smtp = Net::SMTP->new('mailhost', Timeout => 60);
  344.  
  345. =head1 DESCRIPTION
  346.  
  347. This module implements a client interface to the SMTP and ESMTP
  348. protocol, enabling a perl5 application to talk to SMTP servers. This
  349. documentation assumes that you are familiar with the concepts of the
  350. SMTP protocol described in RFC821.
  351.  
  352. A new Net::SMTP object must be created with the I<new> method. Once
  353. this has been done, all SMTP commands are accessed through this object.
  354.  
  355. The Net::SMTP class is a subclass of Net::Cmd and IO::Socket::INET.
  356.  
  357. =head1 EXAMPLES
  358.  
  359. This example prints the mail domain name of the SMTP server known as mailhost:
  360.  
  361.     
  362.     use Net::SMTP;
  363.     
  364.     $smtp = Net::SMTP->new('mailhost');
  365.     print $smtp->domain,"\n";
  366.     $smtp->quit;
  367.  
  368. This example sends a small message to the postmaster at the SMTP server
  369. known as mailhost:
  370.  
  371.     
  372.     use Net::SMTP;
  373.     
  374.     $smtp = Net::SMTP->new('mailhost');
  375.     
  376.     $smtp->mail($ENV{USER});
  377.     $smtp->to('postmaster');
  378.     
  379.     $smtp->data();
  380.     $smtp->datasend("To: postmaster\n");
  381.     $smtp->datasend("\n");
  382.     $smtp->datasend("A simple test message\n");
  383.     $smtp->dataend();
  384.     
  385.     $smtp->quit;
  386.  
  387. =head1 CONSTRUCTOR
  388.  
  389. =over 4
  390.  
  391. =item new Net::SMTP [ HOST, ] [ OPTIONS ]
  392.  
  393. This is the constructor for a new Net::SMTP object. C<HOST> is the
  394. name of the remote host to which a SMTP connection is required.
  395.  
  396. If C<HOST> is not given, then the C<SMTP_Host> specified in C<Net::Config>
  397. will be used.
  398.  
  399. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  400. Possible options are:
  401.  
  402. B<Hello> - SMTP requires that you identify yourself. This option
  403. specifies a string to pass as your mail domain. If not
  404. given a guess will be taken.
  405.  
  406. B<Timeout> - Maximum time, in seconds, to wait for a response from the
  407. SMTP server (default: 120)
  408.  
  409. B<Debug> - Enable debugging information
  410.  
  411.  
  412. Example:
  413.  
  414.  
  415.     $smtp = Net::SMTP->new('mailhost',
  416.                Hello => 'my.mail.domain'
  417.                Timeout => 30,
  418.                            Debug   => 1,
  419.               );
  420.  
  421. =head1 METHODS
  422.  
  423. Unless otherwise stated all methods return either a I<true> or I<false>
  424. value, with I<true> meaning that the operation was a success. When a method
  425. states that it returns a value, failure will be returned as I<undef> or an
  426. empty list.
  427.  
  428. =over 4
  429.  
  430. =item domain ()
  431.  
  432. Returns the domain that the remote SMTP server identified itself as during
  433. connection.
  434.  
  435. =item hello ( DOMAIN )
  436.  
  437. Tell the remote server the mail domain which you are in using the EHLO
  438. command (or HELO if EHLO fails).  Since this method is invoked
  439. automatically when the Net::SMTP object is constructed the user should
  440. normally not have to call it manually.
  441.  
  442. =item mail ( ADDRESS [, OPTIONS] )
  443.  
  444. =item send ( ADDRESS )
  445.  
  446. =item send_or_mail ( ADDRESS )
  447.  
  448. =item send_and_mail ( ADDRESS )
  449.  
  450. Send the appropriate command to the server MAIL, SEND, SOML or SAML. C<ADDRESS>
  451. is the address of the sender. This initiates the sending of a message. The
  452. method C<recipient> should be called for each address that the message is to
  453. be sent to.
  454.  
  455. The C<mail> method can some additional ESMTP OPTIONS which is passed
  456. in hash like fashion, using key and value pairs.  Possible options are:
  457.  
  458.  Size        => <bytes>
  459.  Return      => <???>
  460.  Bits        => "7" | "8"
  461.  Transaction => <ADDRESS>
  462.  Envelope    => <ENVID>
  463.  
  464.  
  465. =item reset ()
  466.  
  467. Reset the status of the server. This may be called after a message has been 
  468. initiated, but before any data has been sent, to cancel the sending of the
  469. message.
  470.  
  471. =item recipient ( ADDRESS [, ADDRESS [ ...]] )
  472.  
  473. Notify the server that the current message should be sent to all of the
  474. addresses given. Each address is sent as a separate command to the server.
  475. Should the sending of any address result in a failure then the
  476. process is aborted and a I<false> value is returned. It is up to the
  477. user to call C<reset> if they so desire.
  478.  
  479. =item to ( ADDRESS [, ADDRESS [...]] )
  480.  
  481. A synonym for C<recipient>.
  482.  
  483. =item data ( [ DATA ] )
  484.  
  485. Initiate the sending of the data from the current message. 
  486.  
  487. C<DATA> may be a reference to a list or a list. If specified the contents
  488. of C<DATA> and a termination string C<".\r\n"> is sent to the server. And the
  489. result will be true if the data was accepted.
  490.  
  491. If C<DATA> is not specified then the result will indicate that the server
  492. wishes the data to be sent. The data must then be sent using the C<datasend>
  493. and C<dataend> methods described in L<Net::Cmd>.
  494.  
  495. =item expand ( ADDRESS )
  496.  
  497. Request the server to expand the given address Returns a reference to an array
  498. which contains the text read from the server.
  499.  
  500. =item verify ( ADDRESS )
  501.  
  502. Verify that C<ADDRESS> is a legitimate mailing address.
  503.  
  504. =item help ( [ $subject ] )
  505.  
  506. Request help text from the server. Returns the text or undef upon failure
  507.  
  508. =item quit ()
  509.  
  510. Send the QUIT command to the remote SMTP server and close the socket connection.
  511.  
  512. =back
  513.  
  514. =head1 SEE ALSO
  515.  
  516. L<Net::Cmd>
  517.  
  518. =head1 AUTHOR
  519.  
  520. Graham Barr <gbarr@ti.com>
  521.  
  522. =head1 COPYRIGHT
  523.  
  524. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  525. This program is free software; you can redistribute it and/or modify
  526. it under the same terms as Perl itself.
  527.  
  528. =cut
  529.